﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BlogSilverlightConsumer.ServiceReference1;


namespace BlogSilverlightConsumer
{
    public partial class Comentarios : UserControl
    {
        public Comentarios()
        {
            InitializeComponent();
            /*
            ComentarioGrid.Columns.Add(new DataGridTextColumn
            {
                Header = "ID",
                Binding = new Binding("Id")
            });

            ComentarioGrid.Columns.Add(new DataGridTextColumn
            {
                Header = "Conteudo",
                Binding = new Binding("Conteudo"),
                Width = new DataGridLength(140)
            });

            ComentarioGrid.Columns.Add(new DataGridTextColumn
            {
                Header = "Autor",
                Binding = new Binding("Autor"),
                Width = new DataGridLength(100)
            });

            ComentarioGrid.Columns.Add(new DataGridTextColumn
            {
                Header = "IdPost",
                Binding = new Binding("Post Id"),
                Width = new DataGridLength(100)
            });
             */
            LimpaTextBox();
            
        }
        private void carregarComentariosGrid()
        {
            ServiceReference1.Service1SoapClient proxy = new Service1SoapClient();

            proxy.retornarTodosComentariosCompleted
                += new EventHandler<retornarTodosComentariosCompletedEventArgs>(comentario_RetornarTodosComentarios);

            proxy.retornarTodosComentariosAsync(Convert.ToInt32(TxtComentarioPostId.Text));
        }

        void comentario_RetornarTodosComentarios(object sender, retornarTodosComentariosCompletedEventArgs e)
        {
            PagedCollectionView pageCollectionView = new PagedCollectionView(e.Result);
            dataPager2.DataContext = pageCollectionView;
            ComentarioGrid.ItemsSource = pageCollectionView;
        }

        private bool Validar()
        {
            if (TxtComentarioId.Text.Trim().Length == 0)
            {
                MessageBox.Show("O ID não pode estar em branco", "Error", MessageBoxButton.OK);
                TxtComentarioId.Focus();
                return false;
            }
            else if (TxtComentarioConteudo.Text.Trim().Length == 0)
            {
                MessageBox.Show("O Conteudo não pode estar em branco", "Error", MessageBoxButton.OK);
                TxtComentarioConteudo.Focus();
                return false;
            }
            else if (TxtComentarioAutor.Text.Trim().Length == 0)
            {
                MessageBox.Show("O Autor não pode estar em branco", "Error", MessageBoxButton.OK);
                TxtComentarioAutor.Focus();
                return false;
            }
            else if (TxtComentarioPostId.Text.Trim().Length == 0)
            {
                MessageBox.Show("A Categoria não pode estar em branco", "Error", MessageBoxButton.OK);
                TxtComentarioPostId.Focus();
                return false;
            }
            else
            {
                return true;
            }
        }

        private void ComentarioGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.MouseLeftButtonUp += new MouseButtonEventHandler(Row_MouseLeftButtonUp);
        }

        void Row_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ServiceReference1.Comentario coment = ComentarioGrid.SelectedItem as ServiceReference1.Comentario;
            TxtComentarioId.Text = "" + coment.Id;
            TxtComentarioConteudo.Text = coment.Conteudo;
            TxtComentarioAutor.Text = coment.Autor;
            TxtComentarioPostId.Text = "" + coment.IdPost;
        }

        private void UpdateButton_Click(object sender, RoutedEventArgs e)
        {
            if (Validar())
            {
                ServiceReference1.Service1SoapClient proxy = new Service1SoapClient();

                proxy.atualizarComentarioCompleted += new EventHandler<atualizarComentarioCompletedEventArgs>
                    (comentarioClient_atualizarComentarioCompleted);
                int id = Convert.ToInt32(TxtComentarioId.Text);
                string conteudo = TxtComentarioConteudo.Text;
                string autor = TxtComentarioAutor.Text;
                string idPost = TxtComentarioPostId.Text;
                proxy.atualizarComentarioAsync(id, conteudo, autor, Convert.ToInt32(idPost));
            }
        }

        void comentarioClient_atualizarComentarioCompleted(object sender, atualizarComentarioCompletedEventArgs e)
        {
            MessageBox.Show(e.Result);
            carregarComentariosGrid();
            LimpaTextBox();
        }

        private void LimpaTextBox()
        {
            TxtComentarioId.Text = string.Empty;
            TxtComentarioConteudo.Text = string.Empty;
            TxtComentarioAutor.Text = string.Empty;
            TxtComentarioPostId.Text = string.Empty;
            TxtComentarioId.Focus();
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            if (TxtComentarioId.Text == "")
            {
                MessageBox.Show("Selecione um registro para deletar", "Deletar", MessageBoxButton.OK);
            }
            else
            {
                if (MessageBox.Show("Confirma exclusão deste registro ? ", "Deletar", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    ServiceReference1.Service1SoapClient proxy = new Service1SoapClient();

                    proxy.removerComentarioCompleted += new EventHandler<removerComentarioCompletedEventArgs>(comentario_removerComentarioCompleted);

                    proxy.removerComentarioAsync(Convert.ToInt32(TxtComentarioId.Text));
                }
            }
        }

        void comentario_removerComentarioCompleted(object sender, removerComentarioCompletedEventArgs e)
        {
            MessageBox.Show(e.Result);
            carregarComentariosGrid();
            LimpaTextBox();
        }

        private void InsertButton_Click(object sender, RoutedEventArgs e)
        {
            if (Validar())
            {
                ServiceReference1.Service1SoapClient proxy = new Service1SoapClient();

                proxy.adicionarComentarioCompleted += new EventHandler<adicionarComentarioCompletedEventArgs>
                    (comentClient_adicionarComentarioCompleted);
                
                string conteudo = TxtComentarioConteudo.Text;
                string autor = TxtComentarioAutor.Text;
                string idPost = TxtComentarioPostId.Text;
                proxy.adicionarComentarioAsync(Convert.ToInt32(idPost), conteudo, autor);
            }
        }

        void comentClient_adicionarComentarioCompleted(object sender, adicionarComentarioCompletedEventArgs e)
        {
            MessageBox.Show(e.Result);
            carregarComentariosGrid();
            LimpaTextBox();
        }

        private void GetButton_Click(object sender, RoutedEventArgs e)
        {
            carregarComentariosGrid();
            LimpaTextBox();
        }

        private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
        {
            App.Navigate(new MainPage());
        }


    }
}
